package com.wisdom.benefit.jufa.utils;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * CompatHelper
 * Created by zhoudawei on 2017/6/13.
 */

public final class CompatHelper {

    public static boolean isEmpty(@Nullable String text) {
        return TextUtils.equals("null", text) || TextUtils.isEmpty(text);
    }

    public static boolean isEmpty(@NonNull TextView textView) {
        return isEmpty(getString(textView));
    }

    public static String getString(@NonNull TextView textView) {
        return textView.getText().toString().trim();
    }

    public static void setEllipsize(@NonNull TextView textView, int maxLength, @NonNull TextUtils.TruncateAt where) {
        textView.setMaxLines(1);
        int width = (int) (textView.getPaddingStart() + textView.getTextSize() * maxLength + textView.getPaddingEnd());
        Drawable[] drawables = textView.getCompoundDrawablesRelative();
        int drawablePadding = textView.getCompoundDrawablePadding();
        if (drawables[0] != null) {
            width += drawables[0].getIntrinsicWidth() + drawablePadding;
        }
        if (drawables[2] != null) {
            width += drawables[2].getIntrinsicWidth() + drawablePadding;
        }
        textView.setMaxWidth(width);
        textView.setEllipsize(where);
        textView.requestLayout();
    }

    public static void setEllipsize(@NonNull TextView textView, int maxLength) {
        setEllipsize(textView, maxLength, TextUtils.TruncateAt.END);
    }
}
